package com.moleq.dialog;

import java.util.List;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.moleq.adapter.SelectionAdapter;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.mgpos.activities.PosSettingActivity;
import com.moleq.mgpos.activities.R;

public class StringSourceDialog extends Dialog implements OnResultListener {

	private OnResultListener mOnResultListener;
	private Button btn_source_ok,btn_source_cancel;
	private ListView lv_dialog_list;
	private Context mContext;
	private List<String> listSource;
	private MessageDialog mMessageDialog;
	private SelectionAdapter selectionAdapter = null;
	private int returnID = 0;
	private String name;
	
	public void onCloseWithReturn(int what,Object obj){
		mOnResultListener.onReturnValue(what,obj);
		dismiss();
	}

	public StringSourceDialog(Context context, Object base) {
		super(context,R.style.dialog2);
		constructor(context,base);
	}
	
	public void setSource(List<String> listSource,String name,int returnID){
		this.listSource = listSource;
		this.name = name;
		this.returnID = returnID;
	}
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public StringSourceDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ps_dialog_string_source);
		this.setCancelable(false);
	
		lv_dialog_list = (ListView) findViewById(R.id.lv_dialog_list);
		btn_source_ok = (Button) findViewById(R.id.btn_source_ok);
		btn_source_cancel = (Button) findViewById(R.id.btn_source_cancel);
		btn_source_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_source_cancel.setOnClickListener(OnClickDialogWindowListener);
		
		selectionAdapter = new SelectionAdapter(mContext, listSource);
		lv_dialog_list.setAdapter(selectionAdapter);
		lv_dialog_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectionAdapter.ScrollToRow(position);
			}
		});
	}
	
	public void show(){
		super.show();	
		switch (returnID) {
		case PosSettingActivity.DIALOG_CURRENCY_SIGN:
			LocateSign(name);
			break;
		default:
			LocateValue(name);
			break;
		}
	}
	
	public void LocateValue(String value) {
		for (int i = 0; listSource.size() > i; i++) {
			if (listSource.get(i).equals(value)) {
				selectionAdapter.ScrollToRow(i);
				return;
			}
		}
		selectionAdapter.ScrollToRow(0);
	}
	
	public void LocateSign(String value) {
		if (value.equals("")) {
			value = "-N/A-";
		}else if (value.equals("$")) {
			value = "$ - US Dollar";
		}else if (value.equals("€")) {
			value = "€ - Euro";
		}
		for (int i = 0; listSource.size() > i; i++) {
			if (listSource.get(i).equals(value)) {
				selectionAdapter.ScrollToRow(i);
				return;
			}
		}
		selectionAdapter.ScrollToRow(0);
	}
	
	public Button.OnClickListener OnClickDialogWindowListener = new Button.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_source_ok:
				if (listSource.size() == 0) {
					onCloseWithReturn(returnID, "");
					dismiss();
				}else {
					String name = listSource.get(selectionAdapter.GetSelectedIndex());
					onCloseWithReturn(returnID, name);
					dismiss();
				}
				break;
			case R.id.btn_source_cancel:
				dismiss();
				break;
			}
		}
	};
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			
		}

		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};
	
	public void ShowMessage(String text,int type,int icon){
		mMessageDialog = new MessageDialog(mContext);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass,
			int viewId, String returnvalue) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub
		
	}
}
